/**
 * Created at 2018/7/12
 * Author: SaintLee
 * <p>
 * Copyright 2018 SaintLee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.com.saintlee.id.impl;

import cn.com.saintlee.id.AbstractIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.stereotype.Service;

import java.util.Calendar;

/**
 * @Description: <p>订单ID生成器</p>
 * @Copyright: <p>Copyright (c) 2018</p>
 * @Company: <p>saintlee.com.cn inc.</p>
 * @date: 2018/7/12 12:28
 * @author: SaintLee
 * @version: v1.0.0
 */
@Service
public class OrderIdGenerator extends AbstractIdGenerator {

    private static final String seqName = "ORDER_ID";

    @Autowired
    public OrderIdGenerator(RedisConnectionFactory connectionFactory) {
        super(seqName, connectionFactory);
    }


    /**
     * 生成序列并按天重置
     */
    public Long nextResetDay() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return next(todayEnd.getTime());
    }
}
